# -*- coding: utf-8 -*-
"""
Created on Wed Aug 15 09:06:34 2018

@author: jiujiujiu

http://www.ipfs.cn/
    IPFS

"""
import requests
from bs4 import BeautifulSoup
from WriteData import writedata
from Tk import genearteMD5


def get_html_text(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'
    }
    try:
        r = requests.get(url, timeout=30, headers=headers)
        r.raise_for_status()
        r.encoding = r.apparent_encoding
        return r.text
    except:
        return None


def get_dates(url):
    tt = get_html_text(url)
    if not tt:
        return []
    text = eval(tt)['content']
    soup = BeautifulSoup(text, 'lxml')
    data = soup.select('.time')
    dates = [date.get_text() for date in data]
    return dates


def count_process():
    a = 0
    dates = []
    while a < 50:
        url = 'http://www.ipfs.cn/Ajax/AjaxNewsFlash.ashx?act=NewsFlash_Index_List&pageindex={}&pagesize=15&Search=2018%25252%25254408%25252%25254415&random=0.4132487270060248'
        url = url.format(a)
        date = get_dates(url)
        dates += date
        a += 1
        print('----IPFS获取第{}页----'.format(a))
    return dates


def count_process_one():  # 快讯
    d1 = 0
    d2 = 0
    d3 = 0
    a = 0
    b = 0
    l = []
    try:
        dates = count_process()
        while b < 3:
            if dates[a] < dates[a + 1]:
                l.append(a + 1)
                b += 1
            a += 1
        d1 = len(dates[:l[0]])
        d2 = len(dates[l[0]:l[1]])
        d3 = len(dates[l[1]:l[2]])
        return (d1, d2, d3)
    except:
        return (d1, d2, d3)


def main():
    u = 'IPFS'
    t1 = count_process_one()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0], "yesterday": t1[1], "frontday": t1[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
